﻿using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace libVNEditor
{
    public class CSentence : Form
    {
        public CSentence()
        {
            InitializeComponent();
        }

        //这个句子的所有事件
        public List<CEvent> events = new List<CEvent>();
        //说话人的名称
        public string Speaker
        {
            set
            {
                this.comboBox_speaker.Text = value;
            }
            get
            {
                return comboBox_speaker.Text;
            }
        }

        //富文本
        public string richText
        {
            set
            {
                this.richTextBox_value.Rtf = value;
            }
            get
            {
                return richTextBox_value.Rtf;
            }
        }
        //纯文本
        public string plainText 
        {
            get
            {
                return richTextBox_value.Text;
            }
            set 
            {
                richTextBox_value.Text = value;
            } 
        }

        public override string ToString()
        {
            string str;
            str = "<sentence speaker=\"" + Speaker + "\">\n";
            str += "<richtext>" + richText + "</richtext>\n";
            str += "<events>\n";
            foreach(CEvent ev in events)
            {
                str += ev.ToString();
            }
            str += "</events>\n</sentence>\n";
            return str;
        }

        private void InitializeComponent()
        {
            this.colorDialog = new System.Windows.Forms.ColorDialog();
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.richTextBox_value = new System.Windows.Forms.RichTextBox();
            this.comboBox_speaker = new System.Windows.Forms.ComboBox();
            this.label3 = new System.Windows.Forms.Label();
            this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel();
            this.checkBox_italic = new System.Windows.Forms.CheckBox();
            this.button_color = new System.Windows.Forms.Button();
            this.checkBox_Bold = new System.Windows.Forms.CheckBox();
            this.tableLayoutPanel3 = new System.Windows.Forms.TableLayoutPanel();
            this.label5 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.tableLayoutPanel4 = new System.Windows.Forms.TableLayoutPanel();
            this.btn_MoveUp = new System.Windows.Forms.Button();
            this.btn_DeleteOption = new System.Windows.Forms.Button();
            this.btn_MoveDown = new System.Windows.Forms.Button();
            this.btn_CreateOption = new System.Windows.Forms.Button();
            this.listBox_events = new System.Windows.Forms.ListBox();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.tableLayoutPanel1.SuspendLayout();
            this.tableLayoutPanel2.SuspendLayout();
            this.tableLayoutPanel3.SuspendLayout();
            this.tableLayoutPanel4.SuspendLayout();
            this.SuspendLayout();
            // 
            // splitContainer1
            // 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.IsSplitterFixed = true;
            this.splitContainer1.Location = new System.Drawing.Point(0, 0);
            this.splitContainer1.Name = "splitContainer1";
            this.splitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal;
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.tableLayoutPanel1);
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this.tableLayoutPanel3);
            this.splitContainer1.Size = new System.Drawing.Size(568, 481);
            this.splitContainer1.SplitterDistance = 263;
            this.splitContainer1.TabIndex = 1;
            // 
            // tableLayoutPanel1
            // 
            this.tableLayoutPanel1.ColumnCount = 2;
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 85F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel1.Controls.Add(this.label1, 0, 0);
            this.tableLayoutPanel1.Controls.Add(this.label2, 0, 1);
            this.tableLayoutPanel1.Controls.Add(this.richTextBox_value, 1, 1);
            this.tableLayoutPanel1.Controls.Add(this.comboBox_speaker, 1, 0);
            this.tableLayoutPanel1.Controls.Add(this.label3, 0, 2);
            this.tableLayoutPanel1.Controls.Add(this.tableLayoutPanel2, 1, 2);
            this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = 3;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 35F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 35F));
            this.tableLayoutPanel1.Size = new System.Drawing.Size(568, 263);
            this.tableLayoutPanel1.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(3, 11);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(79, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "说话人";
            // 
            // label2
            // 
            this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(3, 125);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(79, 12);
            this.label2.TabIndex = 1;
            this.label2.Text = "内容";
            // 
            // richTextBox_value
            // 
            this.richTextBox_value.Dock = System.Windows.Forms.DockStyle.Fill;
            this.richTextBox_value.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.richTextBox_value.Location = new System.Drawing.Point(88, 38);
            this.richTextBox_value.Name = "richTextBox_value";
            this.richTextBox_value.Size = new System.Drawing.Size(477, 187);
            this.richTextBox_value.TabIndex = 2;
            this.richTextBox_value.Text = "";
            this.richTextBox_value.SelectionChanged += new System.EventHandler(this.OnSelectedTextChanged);
            // 
            // comboBox_speaker
            // 
            this.comboBox_speaker.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.comboBox_speaker.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
            this.comboBox_speaker.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
            this.comboBox_speaker.Location = new System.Drawing.Point(88, 7);
            this.comboBox_speaker.Name = "comboBox_speaker";
            this.comboBox_speaker.Size = new System.Drawing.Size(477, 20);
            this.comboBox_speaker.TabIndex = 3;
            // 
            // label3
            // 
            this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(3, 239);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(79, 12);
            this.label3.TabIndex = 4;
            this.label3.Text = "格式控制";
            // 
            // tableLayoutPanel2
            // 
            this.tableLayoutPanel2.ColumnCount = 4;
            this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel2.Controls.Add(this.checkBox_italic, 1, 0);
            this.tableLayoutPanel2.Controls.Add(this.button_color, 2, 0);
            this.tableLayoutPanel2.Controls.Add(this.checkBox_Bold, 0, 0);
            this.tableLayoutPanel2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tableLayoutPanel2.Location = new System.Drawing.Point(88, 231);
            this.tableLayoutPanel2.Name = "tableLayoutPanel2";
            this.tableLayoutPanel2.RowCount = 1;
            this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel2.Size = new System.Drawing.Size(477, 29);
            this.tableLayoutPanel2.TabIndex = 5;
            // 
            // checkBox_italic
            // 
            this.checkBox_italic.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.checkBox_italic.AutoSize = true;
            this.checkBox_italic.Location = new System.Drawing.Point(83, 6);
            this.checkBox_italic.Name = "checkBox_italic";
            this.checkBox_italic.Size = new System.Drawing.Size(74, 16);
            this.checkBox_italic.TabIndex = 1;
            this.checkBox_italic.Text = "倾斜";
            this.checkBox_italic.UseVisualStyleBackColor = true;
            this.checkBox_italic.Click += new System.EventHandler(this.OnItalicChanged);
            // 
            // button_color
            // 
            this.button_color.BackColor = System.Drawing.Color.Black;
            this.button_color.Dock = System.Windows.Forms.DockStyle.Fill;
            this.button_color.Location = new System.Drawing.Point(163, 3);
            this.button_color.Name = "button_color";
            this.button_color.Size = new System.Drawing.Size(74, 23);
            this.button_color.TabIndex = 2;
            this.button_color.UseVisualStyleBackColor = false;
            this.button_color.Click += new System.EventHandler(this.OnColorChanged);
            // 
            // checkBox_Bold
            // 
            this.checkBox_Bold.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.checkBox_Bold.AutoSize = true;
            this.checkBox_Bold.Location = new System.Drawing.Point(3, 6);
            this.checkBox_Bold.Name = "checkBox_Bold";
            this.checkBox_Bold.Size = new System.Drawing.Size(74, 16);
            this.checkBox_Bold.TabIndex = 0;
            this.checkBox_Bold.Text = "加粗";
            this.checkBox_Bold.UseVisualStyleBackColor = true;
            this.checkBox_Bold.Click += new System.EventHandler(this.OnBoldChanged);
            // 
            // tableLayoutPanel3
            // 
            this.tableLayoutPanel3.ColumnCount = 2;
            this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 85F));
            this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel3.Controls.Add(this.label5, 0, 1);
            this.tableLayoutPanel3.Controls.Add(this.label4, 0, 0);
            this.tableLayoutPanel3.Controls.Add(this.tableLayoutPanel4, 1, 0);
            this.tableLayoutPanel3.Controls.Add(this.listBox_events, 1, 1);
            this.tableLayoutPanel3.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tableLayoutPanel3.Location = new System.Drawing.Point(0, 0);
            this.tableLayoutPanel3.Name = "tableLayoutPanel3";
            this.tableLayoutPanel3.RowCount = 2;
            this.tableLayoutPanel3.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 35F));
            this.tableLayoutPanel3.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel3.Size = new System.Drawing.Size(568, 214);
            this.tableLayoutPanel3.TabIndex = 0;
            // 
            // label5
            // 
            this.label5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(3, 118);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(79, 12);
            this.label5.TabIndex = 2;
            this.label5.Text = "事件集合";
            // 
            // label4
            // 
            this.label4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(3, 11);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(79, 12);
            this.label4.TabIndex = 1;
            this.label4.Text = "事件控制";
            // 
            // tableLayoutPanel4
            // 
            this.tableLayoutPanel4.ColumnCount = 5;
            this.tableLayoutPanel4.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel4.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel4.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel4.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel4.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel4.Controls.Add(this.btn_MoveUp, 0, 0);
            this.tableLayoutPanel4.Controls.Add(this.btn_DeleteOption, 0, 0);
            this.tableLayoutPanel4.Controls.Add(this.btn_MoveDown, 1, 0);
            this.tableLayoutPanel4.Controls.Add(this.btn_CreateOption, 0, 0);
            this.tableLayoutPanel4.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tableLayoutPanel4.Location = new System.Drawing.Point(88, 3);
            this.tableLayoutPanel4.Name = "tableLayoutPanel4";
            this.tableLayoutPanel4.RowCount = 1;
            this.tableLayoutPanel4.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel4.Size = new System.Drawing.Size(477, 29);
            this.tableLayoutPanel4.TabIndex = 3;
            // 
            // btn_MoveUp
            // 
            this.btn_MoveUp.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btn_MoveUp.Enabled = false;
            this.btn_MoveUp.Location = new System.Drawing.Point(163, 3);
            this.btn_MoveUp.Name = "btn_MoveUp";
            this.btn_MoveUp.Size = new System.Drawing.Size(74, 23);
            this.btn_MoveUp.TabIndex = 3;
            this.btn_MoveUp.Text = "上移事件";
            this.btn_MoveUp.UseVisualStyleBackColor = true;
            this.btn_MoveUp.Click += new System.EventHandler(this.MoveUpEventClick);
            // 
            // btn_DeleteOption
            // 
            this.btn_DeleteOption.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btn_DeleteOption.Enabled = false;
            this.btn_DeleteOption.Location = new System.Drawing.Point(83, 3);
            this.btn_DeleteOption.Name = "btn_DeleteOption";
            this.btn_DeleteOption.Size = new System.Drawing.Size(74, 23);
            this.btn_DeleteOption.TabIndex = 2;
            this.btn_DeleteOption.Text = "删除事件";
            this.btn_DeleteOption.UseVisualStyleBackColor = true;
            this.btn_DeleteOption.Click += new System.EventHandler(this.DeleteEventClick);
            // 
            // btn_MoveDown
            // 
            this.btn_MoveDown.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btn_MoveDown.Enabled = false;
            this.btn_MoveDown.Location = new System.Drawing.Point(243, 3);
            this.btn_MoveDown.Name = "btn_MoveDown";
            this.btn_MoveDown.Size = new System.Drawing.Size(74, 23);
            this.btn_MoveDown.TabIndex = 1;
            this.btn_MoveDown.Text = "下移事件";
            this.btn_MoveDown.UseVisualStyleBackColor = true;
            this.btn_MoveDown.Click += new System.EventHandler(this.MoveDownEventClick);
            // 
            // btn_CreateOption
            // 
            this.btn_CreateOption.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btn_CreateOption.Location = new System.Drawing.Point(3, 3);
            this.btn_CreateOption.Name = "btn_CreateOption";
            this.btn_CreateOption.Size = new System.Drawing.Size(74, 23);
            this.btn_CreateOption.TabIndex = 0;
            this.btn_CreateOption.Text = "创建事件";
            this.btn_CreateOption.UseVisualStyleBackColor = true;
            this.btn_CreateOption.Click += new System.EventHandler(this.CreateEventClick);
            // 
            // listBox_events
            // 
            this.listBox_events.Dock = System.Windows.Forms.DockStyle.Fill;
            this.listBox_events.FormattingEnabled = true;
            this.listBox_events.ItemHeight = 12;
            this.listBox_events.Location = new System.Drawing.Point(88, 38);
            this.listBox_events.Name = "listBox_events";
            this.listBox_events.Size = new System.Drawing.Size(477, 173);
            this.listBox_events.TabIndex = 4;
            this.listBox_events.SelectedIndexChanged += new System.EventHandler(this.listBox_actions_SelectedIndexChanged);
            this.listBox_events.DoubleClick += new System.EventHandler(this.OnEdit);
            // 
            // CSentence
            // 
            this.ClientSize = new System.Drawing.Size(568, 481);
            this.Controls.Add(this.splitContainer1);
            this.Name = "CSentence";
            this.Text = "句子";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.OnFormClosing);
            this.Load += new System.EventHandler(this.cSentence_Load);
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
            this.splitContainer1.ResumeLayout(false);
            this.tableLayoutPanel1.ResumeLayout(false);
            this.tableLayoutPanel1.PerformLayout();
            this.tableLayoutPanel2.ResumeLayout(false);
            this.tableLayoutPanel2.PerformLayout();
            this.tableLayoutPanel3.ResumeLayout(false);
            this.tableLayoutPanel3.PerformLayout();
            this.tableLayoutPanel4.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #region 定义组件
        private SplitContainer splitContainer1;
        private TableLayoutPanel tableLayoutPanel1;
        private Label label1;
        private Label label2;
        private RichTextBox richTextBox_value;
        private ComboBox comboBox_speaker;
        private Label label3;
        private TableLayoutPanel tableLayoutPanel2;
        private CheckBox checkBox_italic;
        private Button button_color;
        private CheckBox checkBox_Bold;
        private TableLayoutPanel tableLayoutPanel3;
        private Label label5;
        private Label label4;
        private TableLayoutPanel tableLayoutPanel4;
        private Button btn_MoveUp;
        private Button btn_DeleteOption;
        private Button btn_MoveDown;
        private Button btn_CreateOption;
        private ListBox listBox_events;
        private ColorDialog colorDialog;
        #endregion

        public void RefreshForm()
        {
            listBox_events.Items.Clear();
            foreach (CEvent ev in events)
            {
                listBox_events.Items.Add(string.Format("[{0}]{1}", ev.type, ev.name));
            }

            comboBox_speaker.Items.Clear();
            comboBox_speaker.AutoCompleteCustomSource.Clear();
            foreach(Character c in Program.mainform.Plot.characters)
            {
                comboBox_speaker.Items.Add(c.name);
                comboBox_speaker.AutoCompleteCustomSource.Add(c.name);
            }
        }

        private void OnSelectedTextChanged(object sender, EventArgs e)
        {
            if (richTextBox_value.SelectedText == "") return;
            if (richTextBox_value.SelectionFont == null) return;
            checkBox_Bold.Checked = richTextBox_value.SelectionFont.Bold;
            checkBox_italic.Checked = richTextBox_value.SelectionFont.Italic;
            button_color.BackColor = richTextBox_value.SelectionColor;
            //Console.WriteLine(richTextBox_value.SelectedRtf);
        }

        private void OnBoldChanged(object sender, EventArgs e)
        {
            if (richTextBox_value.SelectedText == "") return;
            if (richTextBox_value.SelectionFont == null) return;
            Font orign_font = richTextBox_value.SelectionFont;
            Font font;
            if (checkBox_Bold.Checked)
                font = new Font(orign_font, orign_font.Style | FontStyle.Bold);
            else
                font = new Font(orign_font, orign_font.Style & ~FontStyle.Bold) ;
            richTextBox_value.SelectionFont = font;
            richTextBox_value.Focus();
        }

        private void OnItalicChanged(object sender, EventArgs e)
        {
            if (richTextBox_value.SelectedText == "") return;
            if (richTextBox_value.SelectionFont == null) return;
            Font orign_font = richTextBox_value.SelectionFont;
            Font font;
            if (checkBox_italic.Checked)
                font = new Font(orign_font, orign_font.Style | FontStyle.Italic);
            else
                font = new Font(orign_font, orign_font.Style & ~FontStyle.Italic);
            richTextBox_value.SelectionFont = font;
            richTextBox_value.Focus();
        }
        
        private void OnColorChanged(object sender, EventArgs e)
        {
            if (richTextBox_value.SelectedText == "") return;
            if (richTextBox_value.SelectionColor == null) return;
            richTextBox_value.SelectionBackColor = Color.FromArgb(128, 255, 255);
            colorDialog.ShowDialog();
            richTextBox_value.SelectionColor = colorDialog.Color;
            button_color.BackColor = colorDialog.Color;
            richTextBox_value.SelectionBackColor = Color.FromArgb(255, 255, 255);
            richTextBox_value.Focus();
        }

        private void OnFormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Visible = false;
        }

        private void listBox_actions_SelectedIndexChanged(object sender, EventArgs e)
        {
            int idx = listBox_events.SelectedIndex;
            if (idx == -1)
            {
                btn_MoveUp.Enabled = false;
                btn_MoveDown.Enabled = false;
                btn_DeleteOption.Enabled = false;
            }
            else if (idx == 0)
            {
                btn_DeleteOption.Enabled = true;
                if (idx == listBox_events.Items.Count - 1)
                {
                    btn_MoveUp.Enabled = false;
                    btn_MoveDown.Enabled = false;
                }
                else if (idx < listBox_events.Items.Count - 1)
                {
                    btn_MoveDown.Enabled = true;
                    btn_MoveUp.Enabled = false;
                }
            }
            else if (idx == listBox_events.Items.Count - 1)
            {
                btn_DeleteOption.Enabled = true;
                if (idx == 0)
                {
                    btn_MoveUp.Enabled = false;
                    btn_MoveDown.Enabled = false;
                }
                else if (idx > 0)
                {
                    btn_MoveDown.Enabled = false;
                    btn_MoveUp.Enabled = true;
                }
            }
            else
            {
                btn_DeleteOption.Enabled = true;
                btn_MoveUp.Enabled = true;
                btn_MoveDown.Enabled = true;
            }
        }

        private void CreateEventClick(object sender, EventArgs e)
        {
            string name = Interaction.InputBox("请输入事件名称：");
            if (name == "") return;
            CEvent ev = new CEvent() { name = name };
            events.Add(ev);
            RefreshForm();
        }

        private void OnEdit(object sender, EventArgs e)
        {
            int idx = listBox_events.SelectedIndex;
            if (idx == -1) return;
            events[idx].ShowDialog();
            RefreshForm();
        }

        private void DeleteEventClick(object sender, EventArgs e)
        {
            int idx = listBox_events.SelectedIndex;
            if (idx == -1) return;
            DialogResult result = MessageBox.Show("确定要删除这个事件吗?", "删除事件", MessageBoxButtons.OKCancel);
            if (result == DialogResult.OK)
            {
                events.RemoveAt(idx);
                RefreshForm();
            }
        }

        private void MoveUpEventClick(object sender, EventArgs e)
        {
            int idx = listBox_events.SelectedIndex;
            CEvent s1 = events[idx];
            CEvent s2 = events[idx - 1];
            events[idx] = s2;
            events[idx - 1] = s1;
            RefreshForm();
        }

        private void MoveDownEventClick(object sender, EventArgs e)
        {
            int idx = listBox_events.SelectedIndex;
            CEvent s1 = events[idx];
            CEvent s2 = events[idx + 1];
            events[idx] = s2;
            events[idx + 1] = s1;
            RefreshForm();
        }

        private void cSentence_Load(object sender, EventArgs e)
        {
            RefreshForm();
        }
    }
}
